﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.EventAdvertising;

namespace FwProject.Manager
{
    public class ShopUserCouponManager : IDomainService
    {
        private readonly IRepository<ShopUserCoupon, Guid> _shopUserCouponRepository;

        public ShopUserCouponManager(IRepository<ShopUserCoupon, Guid> shopUserCouponRepository)
        {
            _shopUserCouponRepository = shopUserCouponRepository;
        }
        /// <summary>
        ///派发给用户一个优惠券
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateShopUserCoupon(ShopUserCoupon model)
        {
        
            return await _shopUserCouponRepository.InsertAndGetIdAsync(model);
        }
        /// <summary>
        ///  修改用户优惠券
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task UpdateShopUserCoupon(ShopUserCoupon model)
        {
            await _shopUserCouponRepository.UpdateAsync(model);
        }
        /// <summary>
        /// 订单使用优惠券
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ShopUserCoupon UserShopUserCoupon(Guid Id,string OrderNumber)
        {
            var model = _shopUserCouponRepository.GetAllIncluding(o => o.ShopCoupon).Where(o => o.Id == Id).FirstOrDefault();
            if(model==null)
                throw new UserFriendlyException("此优惠券不存在");

            if (model.UseStatus == UseStatus.Used)
                 throw new UserFriendlyException("此优惠券已使用过");

            model.OrderNumber = OrderNumber;
            return model;
        }
        /// <summary>
        /// 退回订单优惠券退回
        /// </summary>
        /// <param name="OrderNumber"></param>
        public void UserShopBackCoupon(string OrderNumber)
        {
            var model =  _shopUserCouponRepository.GetAll().Where(o => o.OrderNumber == OrderNumber);
            foreach (var item in model)
            {
                item.OrderNumber = string.Empty;
            }

        }
        /// <summary>
        /// 优惠券成功使用
        /// </summary>
        /// <param name="OrderNumber"></param>
        public void UserShopUserCoupon(string OrderNumber)
        {
            var model = _shopUserCouponRepository.GetAll().Where(o => o.OrderNumber == OrderNumber);
            foreach (var item in model)
            {
                item.UseStatus = UseStatus.Used;
            }

        }


        /// <summary>
        /// 删除用户的一个优惠券
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteShopUserCoupon(ShopUserCoupon model)
        {
            model.UseStatus = UseStatus.Delete;
            await _shopUserCouponRepository.UpdateAsync(model);
        }
    }
}
